package com.wip.reportingservice.services.impls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wip.reportingservice.daos.entities.ClientsTable;
import com.wip.reportingservice.daos.entities.PageClientTimeTable;
import com.wip.reportingservice.daos.entities.ReportDataTable;
import com.wip.reportingservice.daos.interfaces.IClientsDao;
import com.wip.reportingservice.daos.interfaces.ILogsDao;
import com.wip.reportingservice.daos.interfaces.IPageClientTimeDao;
import com.wip.reportingservice.daos.interfaces.IPagesDao;
import com.wip.reportingservice.daos.interfaces.IReportDataDao;
import com.wip.reportingservice.services.domains.ReportData;
import com.wip.reportingservice.services.interfaces.IReportService;

@Service
public class ReportServiceImpl implements IReportService {

	@Autowired
	private IClientsDao clientDao;

	@Autowired
	private IPagesDao pageDao;

	@Autowired
	private ILogsDao logDao;

	@Autowired
	private IPageClientTimeDao recordsDao;
	
	@Autowired
	private IReportDataDao reportDao;

	public String getReport() {
		// get me client with id 1
		ClientsTable clent = clientDao.getById(1);

		// return the client name
		return clent.getClientName();
	}

	public ReportData getOtherReport(List<String> clients, List<String> pages,
			List<String> logs) {

		HashMap<String, HashMap<String, HashMap<String, ArrayList<Integer>>>> clientMap = new HashMap<String, HashMap<String, HashMap<String, ArrayList<Integer>>>>();

		for (String clientName : clients) {
			int clientId = clientDao.getByName(clientName).getClientId();
			String clientKey = String.valueOf(clientId);

			HashMap<String, HashMap<String, ArrayList<Integer>>> logsMap = new HashMap<String, HashMap<String, ArrayList<Integer>>>();
			for (String log : logs) {
				int logId = logDao.getByName(log).getLogId();
				String logKey = String.valueOf(logId);

				HashMap<String, ArrayList<Integer>> pagesMap = new HashMap<String, ArrayList<Integer>>();
				for (String page : pages) {
					int pageId = pageDao.getByName(page).getPageId();
					String pageKey = String.valueOf(pageId);

					// execute the query and save it to the map.
					// execute and save to the map

					List<PageClientTimeTable> records = recordsDao.getRecord(
							clientId, logId, pageId);

					for (PageClientTimeTable record : records) {
						int time = records.get(0).getAccessTime();

						if (clientMap.get(clientName) == null) {
							// need new map to the end

							ArrayList<Integer> timeList = new ArrayList<Integer>();
							timeList.add(time);

							pagesMap = new HashMap<String, ArrayList<Integer>>();
							pagesMap.put(page, timeList);

							logsMap = new HashMap<String, HashMap<String, ArrayList<Integer>>>();
							logsMap.put(log, pagesMap);

							clientMap.put(clientName, logsMap);

						} else {
							// there is a client, get the map
							logsMap = clientMap.get(clientName);

							if (logsMap.get(log) == null) {
								ArrayList<Integer> timeList = new ArrayList<Integer>();
								timeList.add(time);

								pagesMap = new HashMap<String, ArrayList<Integer>>();
								pagesMap.put(page, timeList);

								logsMap.put(log, pagesMap);
							} else {
								// there is a logs map, get it
								pagesMap = logsMap.get(log);

								if (pagesMap.get(page) == null) {
									ArrayList<Integer> timeList = new ArrayList<Integer>();
									timeList.add(time);

									pagesMap.put(page, timeList);
								} else {
									// there is a pages map, get it time list
									ArrayList<Integer> timeList = pagesMap
											.get(page);
									timeList.add(time);

								}

							}

						}

					}
					// get map and add

				}
			}
		}
		ReportData rpt = new ReportData();

		rpt.setDataMap(clientMap);

		return rpt;

	}
	
	
	public ReportData getOtherReportNew(List<String> clients, List<String> pages,
			List<String> logs) {

		HashMap<String, HashMap<String, HashMap<String, ArrayList<Integer>>>> clientMap = new HashMap<String, HashMap<String, HashMap<String, ArrayList<Integer>>>>();
		HashMap<String, HashMap<String, ArrayList<Integer>>> logsMap = new HashMap<String, HashMap<String, ArrayList<Integer>>>();
		HashMap<String, ArrayList<Integer>> pagesMap = new HashMap<String, ArrayList<Integer>>();
		//ArrayList<Integer> timeList = new ArrayList<Integer>();
		
		List<ReportDataTable> records = reportDao.getRecords(pages, clients, logs);
		
		for(ReportDataTable resultRow:records)
		{
			String clientKey = resultRow.getClientName();
			String logKey = resultRow.getLogName().toString();
			String pageKey = resultRow.getPageName();
			int time =  resultRow.getAccessTime();
			
			if (clientMap.get(clientKey) == null) {
				// need new map to the end

				ArrayList<Integer> timeList = new ArrayList<Integer>();
				timeList.add(time);

				pagesMap = new HashMap<String, ArrayList<Integer>>();
				pagesMap.put(pageKey, timeList);

				logsMap = new HashMap<String, HashMap<String, ArrayList<Integer>>>();
				logsMap.put(logKey, pagesMap);

				clientMap.put(clientKey, logsMap);

			} else {
				// there is a client, get the map
				logsMap = clientMap.get(clientKey);

				if (logsMap.get(logKey) == null) {
					ArrayList<Integer> timeList = new ArrayList<Integer>();
					timeList.add(time);

					pagesMap = new HashMap<String, ArrayList<Integer>>();
					pagesMap.put(pageKey, timeList);

					logsMap.put(logKey, pagesMap);
				} else {
					// there is a logs map, get it
					pagesMap = logsMap.get(logKey);

					if (pagesMap.get(pageKey) == null) {
						ArrayList<Integer> timeList = new ArrayList<Integer>();
						timeList.add(time);

						pagesMap.put(pageKey, timeList);
					} else {
						// there is a pages map, get it time list
						ArrayList<Integer> timeList = pagesMap
								.get(pageKey);
						timeList.add(time);

					}

				}

			}
			
		}
				
		ReportData rpt = new ReportData();

		rpt.setDataMap(clientMap);

		return rpt;

	}

	@Override
	public ReportData getOtherReport() {
		// get all times for pageid=1, clientid=1, logid=1

		List<PageClientTimeTable> records = recordsDao.getRecord(1, 1, 1);

		StringBuilder time = new StringBuilder();

		for (PageClientTimeTable record : records) {
			time.append(record.getAccessTime() + ",");
		}

		// PageClientTimeTable record = recordsDao.getById(1);

		// time.append(record.getAccessTime());
		ReportData rpt = new ReportData();

		rpt.setHeader(time);

		return rpt;
	}
	/*
	 * @Override public ReportData getOtherOtherReport() { // get all times for
	 * pageid=1, clientid=1, logid=1
	 * 
	 * List<Integer> pages = new ArrayList<Integer>(); pages.add(1);
	 * 
	 * List<Integer> clients = new ArrayList<Integer>(); clients.add(1);
	 * 
	 * List<Integer> logs = new ArrayList<Integer>(); clients.add(1);
	 * 
	 * List<PageClientTimeTable> records = recordsDao.getRecord(pages, clients,
	 * logs);
	 * 
	 * // return this map to report data Map<String, Map<String, Map<String,
	 * List<Integer>>>> clientMap = new HashMap<String, Map<String, Map<String,
	 * List<Integer>>>>();
	 * 
	 * // map<Client,Map<Log, Map<Page,List<Integer> times>>>.
	 * 
	 * for (PageClientTimeTable record : records) { // one to many-many to one,
	 * to prevent more trips to db String clientName =
	 * record.getClients().getClientName(); String logName =
	 * record.getLogs().getLogFileDate().toString(); String pageName =
	 * record.getPages().getPageName(); int time = record.getAccessTime();
	 * 
	 * if (clientMap.get(clientName) == null) { // need new map to the end
	 * 
	 * List<Integer> timeList = new ArrayList<Integer>(); timeList.add(time);
	 * 
	 * Map<String, List<Integer>> pagesMap = new HashMap<String,
	 * List<Integer>>(); pagesMap.put(pageName, timeList);
	 * 
	 * Map<String, Map<String, List<Integer>>> logsMap = new HashMap<String,
	 * Map<String, List<Integer>>>(); logsMap.put(logName, pagesMap);
	 * 
	 * clientMap.put(clientName, logsMap);
	 * 
	 * } else { // there is a client, get the map Map<String, Map<String,
	 * List<Integer>>> logsMap = clientMap .get(clientName);
	 * 
	 * if (logsMap.get(logName) == null) { List<Integer> timeList = new
	 * ArrayList<Integer>(); timeList.add(time);
	 * 
	 * Map<String, List<Integer>> pagesMap = new HashMap<String,
	 * List<Integer>>(); pagesMap.put(pageName, timeList);
	 * 
	 * logsMap.put(logName, pagesMap); } else { // there is a logs map, get it
	 * Map<String, List<Integer>> pagesMap = logsMap.get(logName);
	 * 
	 * if (pagesMap.get(pageName) == null) { List<Integer> timeList = new
	 * ArrayList<Integer>(); timeList.add(time);
	 * 
	 * pagesMap.put(pageName, timeList); } else { // there is a pages map, get
	 * it time list List<Integer> timeList = pagesMap.get(pageName);
	 * timeList.add(time);
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 * 
	 * 
	 * } ReportData rpt = new ReportData();
	 * 
	 * rpt.setDataMap(clientMap);
	 * 
	 * return rpt;
	 * 
	 * }
	 */
}
